﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.BLL;
using OLMS.DAL;

namespace OLMS
{
    public partial class Cart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CartsBinder();
            }
        }
        private void CartsBinder()
        {
            Users user = Session["user"] as Users;
            if (user != null)
            {
                List<Carts> cartList = CartBLL.GetCarts(user.UserId);
                var list = cartList.Select(c => new
                {
                    CartId=c.CartId,
                    AlbumId=c.AlbumId,
                    AlbumsTitle=c.Albums.Title,
                    ArtistName= ArtistBLL.GetArtistsById(c.Albums.ArtistId).Name,
                    Price=string.Format("{0:C}元",c.Albums.Price),
                    Count=c.Count,
                    ItemPrice= string.Format("{0:C}元", c.Albums.Price*c.Count)
                }).ToList();
                repAlbumsList.DataSource = list;
                repAlbumsList.DataBind();
                lblTotal.Text=string.Format("总计：{0:C}元",CartBLL.GetTotal(user.UserId));
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
          
        }
       
        protected void btnRemove_Command(object sender, CommandEventArgs e)
        {
            Users user = Session["user"] as Users;
            int cartId = Convert.ToInt32(e.CommandArgument);
            CartBLL.RemoveFromCart(user.UserId, cartId);
            CartsBinder();
        }
        //结算
        protected void btnOrder_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrderItem.aspx");
        }
        //清空购物车
        protected void btnClearCart_Click(object sender, EventArgs e)
        {
            Users user = Session["user"] as Users;
            CartBLL.EmptyCart(user.UserId);
            CartsBinder();
        }
    }
}